<?php
/**
 * 商铺资料修改审核
 * @author chenyangbj8826
 *
 */
class Controller_Admin_Shoplog  extends Controller_Template {

	private $page_per_count = 10; //每页显示条数
	public function __construct($request,$response)
	{
		parent::__construct($request,$response);
		$this->template	= ('smarty:admin/shop/log');	
	}

	/**
	 * 界面展示
	 */
	public function action_index()
	{
		//如何查询条件为空则为查询全部
		$query_type = $this->request->query('query') ? $this->request->query('query') : 'all';
		$shop_name = (string) $this->request->query('search');
		$current_sort = $this->request->query('sort') ? $this->request->query('sort') : '1';

		$page_no = intval($this->request->query('page')) ? intval($this->request->query('page')) : 1;
		$page_no < 0 && $page_no = 1;
		$start_num = ($page_no -1) * $this->page_per_count;
		$arr_shop_ids = strlen(trim($shop_name)) ? $this->get_shop_ids(trim($shop_name)) : '';

		//获得日志数组
		$query = array();
		($query_type!='all') && $query['_in']['status'] = array(Kohana::config('shop.shop_audit.'.$query_type));		
		is_array($arr_shop_ids) && $query['_in']['shop_id'] = $arr_shop_ids;
		$current_sort == 2 && $query['_sort'] = array('shop_id'=>-1,'_id'=>-1); 
		$arr_shop_logs = $this->get_shop_logs($query,$start_num,$this->page_per_count);

		$this->template->set('current_query',$query_type);
		$this->template->set('pageno',$page_no);
		$this->template->set('logs',$arr_shop_logs);
		$this->template->set('current_search',$shop_name);
		$this->template->set('delete_reasons',$this->get_reasons());
		$this->template->set('current_sort',$current_sort);
		$this->template->set('pre_page',$page_no-1);
		$this->template->set('next_page',(count($arr_shop_logs)-$this->page_per_count)<0 ? -1 : $page_no+1);
		$this->template->set_filename('smarty:admin/shop/log');

	}
	
	/**
	 * 删除店铺资料的修改日志
	 * @param  $shop_log_id
	 */
	public function action_delete($shop_log_id,$status,$reason_id)
	{
		//是否删除成功都刷新界面
		$Shop_Log = new Shop_Log;
		$Shop_Log->delete($shop_log_id,$status,$reason_id);
		$this->redirect_to_index();
	}
	
	/**
	 * 恢复店铺资料的修改
	 * @param  $shop_log_id
	 */
	public function action_recover($shop_log_id,$status)
	{
		//是否恢复成功都刷新界面
		$Shop_Log = new Shop_Log;
		$Shop_Log->recovery($shop_log_id,$status);
		$this->redirect_to_index();
	}

	/**
	 * 认证店铺资料修改日志
	 * @param  $shop_log_id
	 */
	public function action_authorize($shop_log_id,$status)
	{
		//是否认证成功都刷新界面
		$Shop_Log = new Shop_Log;
		$Shop_Log->anthorized($shop_log_id,$status);
		$this->redirect_to_index();
	}

	/**
	 * 取消认证店铺资料修改日志
	 * @param  $shop_log_id
	 */
	public function action_unauthorize($shop_log_id,$status)
	{	
		//是否取消认证成功都刷新界面
		$Shop_Log = new Shop_Log;
		$Shop_Log->unanthorized($shop_log_id,$status); 
		$this->redirect_to_index();
	}
	
	/**
	 * 页面跳转
	 */
	private function redirect_to_index()
	{
		$query_type = $this->request->query('query') ? $this->request->query('query') : ( $this->request->post('query') ? $this->request->post('query') : 'all');
		$shop_name = $this->request->query('search') ? $this->request->query('search') :$this->request->post('search');
		$current_sort = $this->request->query('sort') ? $this->request->query('sort') :( $this->request->post('sort') ? $this->request->post('sort') : '1');

		$url='/shoplog?query='.$query_type.'&search='.$shop_name.'&sort='.$current_sort;
		$this->request->redirect($url,302);
	}
	/**
	 * 根据店铺名称字符串获得的所有包括该字符串的店铺id
	 * @param unknown_type $shop_name
	 */
	private function get_shop_ids($shop_name,$nums = 10)
	{
		$query['shop_name'] = $shop_name;
		$arr_result = Shop::search($query);
		$str_shop_ids = '';
		$array_ids = array();
		foreach($arr_result['list'] as $item)
		{
			in_array($item['id'], $array_ids) ||  $array_ids[] = intval($item['id']);
		}
		return $array_ids;		
	}
	
	
	/**
	 * 获得商铺日志信息
	 * @param array $query 查询条件
	 * @param unknown_type $start 开始的记录，第一条从0开始
	 * @param unknown_type $limit 查询的记录数
	 * @return 商铺日志信息数组
	 */
	private function get_shop_logs(array $query=NULL,$start,$limit)
	{		
		// 加工数组
		$Shop_Log = new Shop_Log;
		$arr_result = $Shop_Log->search($query,$start,$limit);
		if(!empty($arr_result) && is_array($arr_result) && count($arr_result)>0)
		{
			$arr_shoplogs_info= &$arr_result;
			foreach ($arr_shoplogs_info as $key=>$value)
			{
				if($arr_shoplogs_info[$key])
				{
					//根据shop_id获得店铺名称等信息
					$arr_shoplogs_info[$key]['shop_name'] = Shop::name($arr_shoplogs_info[$key]['shop_id']);
					$arr_shoplogs_info[$key]['shop_url'] = Shop::shop_detail_url($arr_shoplogs_info[$key]['shop_id']);
					
					//根据K_type获得log_str_old或log_img_old_url,log_str_new或log_img_new_url
					$config_image=Kohana::config('shop.shop_information.'.$arr_shoplogs_info[$key]['k_type'].'.is_img');
					$arr_shoplogs_info[$key]['is_image'] = $config_image;
					$arr_shoplogs_info[$key]['log_str_old'] = $config_image ? '' : $arr_shoplogs_info[$key]['old'];
					$arr_shoplogs_info[$key]['log_img_old_url'] = $config_image ? Service_Image::url($arr_shoplogs_info[$key]['old']['img_id'].'.'.$arr_shoplogs_info[$key]['old']['img_postfix']) : '';
					$arr_shoplogs_info[$key]['log_str_new'] = $config_image ? '' : $arr_shoplogs_info[$key]['new'];
					$arr_shoplogs_info[$key]['log_img_new_url'] = $config_image ? Service_Image::url($arr_shoplogs_info[$key]['new']['img_id'].'.'.$arr_shoplogs_info[$key]['new']['img_postfix']) : '';
					
					//根据type获得商铺资料日志类型名称
					$arr_shoplogs_info[$key]['shopinfo_type'] = Kohana::config('shop.shop_information.'.$arr_shoplogs_info[$key]['k_type'].'.name');
				}
			}
		}
		return $arr_result;		
	}
	
	
	private function get_reasons()
	{
		$arr_reason = Kohana::config('shop.log_delete_reason');
		return $arr_reason;
	}
	
}
